package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.SeelBean;
import com.wnhz.work.yz.bean.SelectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectProjectActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler, tao;
    private TextView taocan, jushi1, price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_project);
        initTitle();
        taocan = (TextView) findViewById(R.id.taocan);
        jushi1 = (TextView) findViewById(R.id.jushi1);
        price = (TextView) findViewById(R.id.price);
        //
        recycler = (RecyclerView) findViewById(R.id.jushi);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.text)));
        tao = (RecyclerView) findViewById(R.id.tao);
        tao.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        upLoad();


    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", getIntent().getStringExtra("pid"));
        showDialog();

        XUtil.Post(Url.DECORATE_PACKAGETYPE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        SeelBean bean = gson.fromJson(result, SeelBean.class);
                        List<SeelBean.InfoEntity> info1 = bean.getInfo();
                        setData(info1);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    BaseRVAdapter taoadapter;

    private void setData(final List<SeelBean.InfoEntity> info1) {
        if (info1 != null && info1.size() > 0) {
            info1.get(0).setStatus(1);
            taocan.setText("" + info1.get(0).getClassname());
            cate = info1.get(0).getCate();
            info1.get(0).getCate().get(0).setStatus(1);
            selectId = info1.get(0).getCate().get(0).getFid();
            jushi1.setText("" + info1.get(0).getCate().get(0).getClassname());
            price.setText("" + info1.get(0).getCate().get(0).getPrice() + "/" + info1.get(0).getCate().get(0).getUnit());
        }
        taoadapter = new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_tao;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                TextView text = holder.getTextView(R.id.text);
                text.setText(info1.get(position).getClassname());
                if (1 == info1.get(position).getStatus()) {
                    text.setTextColor(getResources().getColor(R.color.main_color));
                } else {
                    text.setTextColor(getResources().getColor(R.color.text));
                }
                text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < info1.size(); i++) {
                            for (int j = 0; j < info1.get(i).getCate().size(); j++) {
                                info1.get(i).getCate().get(j).setStatus(0);
                            }
                        }

                        taocan.setText("" + info1.get(position).getClassname());
                        final List<SeelBean.InfoEntity.CateEntity> cate = info1.get(position).getCate();

                        if (cate != null && cate.size() > 0) {
                            cate.get(0).setStatus(1);
                            selectId = cate.get(0).getFid();
                        }
                        Log.e("---", cate + "--" + cate.size());
                        for (int i = 0; i < info1.size(); i++) {
                            if (i == position) {
                                info1.get(i).setStatus(1);
                            } else {
                                info1.get(i).setStatus(0);
                            }
                        }
                        try {
                            jushi1.setText("" + info1.get(position).getCate().get(0).getClassname());
                            price.setText("" + info1.get(position).getCate().get(0).getPrice() + "/" + info1.get(0).getCate().get(0).getUnit());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        taoadapter.notifyDataSetChanged();
                        adapter = new BaseRVAdapter(SelectProjectActivity.this, cate) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.jushi;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {
                                try {
                                    TextView textView = holder.getTextView(R.id.text);
                                    textView.setText(cate.get(position).getClassname());
                                    ImageView select = holder.getImageView(R.id.select);
                                    if (0 == cate.get(position).getStatus()) {
                                        select.setImageResource(R.mipmap.ic_choose_nor);
                                    } else {
                                        select.setImageResource(R.mipmap.ic_choose_li);
                                    }


                                    holder.getView(R.id.ll_select).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            for (int i = 0; i < cate.size(); i++) {
                                                if (i == position) {
                                                    cate.get(i).setStatus(1);
                                                } else {
                                                    cate.get(i).setStatus(0);
                                                }
                                            }
                                            selectId = cate.get(position).getFid();
                                            // cate.get(position).setStatus(cate.get(position).getStatus() == 1 ? 0 : 1);
                                            jushi1.setText("" + cate.get(position).getClassname());
                                            price.setText("" + cate.get(position).getPrice() + "/" + cate.get(position).getUnit());
                                            adapter.notifyDataSetChanged();

                                        }
                                    });

                                    textView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            getDeta(getIntent().getStringExtra("pid"), cate.get(position).getFid());

                                        }
                                    });
                                } catch (Exception r) {
                                }
                            }

                        };
                        recycler.setAdapter(adapter);
                    }
                });

            }
        };
        tao.setAdapter(taoadapter);
        adapter = new BaseRVAdapter(this, cate) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.jushi;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                try {
                    TextView textView = holder.getTextView(R.id.text);
                    textView.setText(cate.get(position).getClassname());
                    ImageView select = holder.getImageView(R.id.select);
                    if (0 == cate.get(position).getStatus()) {
                        select.setImageResource(R.mipmap.ic_choose_nor);
                    } else {
                        select.setImageResource(R.mipmap.ic_choose_li);
                    }


                    holder.getView(R.id.ll_select).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            for (int i = 0; i < cate.size(); i++) {
                                if (i == position) {
                                    cate.get(i).setStatus(1);
                                } else {
                                    cate.get(i).setStatus(0);

                                }
                            }
                            selectId = cate.get(position).getFid();

                            // cate.get(position).setStatus(cate.get(position).getStatus() == 1 ? 0 : 1);
                            jushi1.setText("" + cate.get(position).getClassname());
                            price.setText("" + cate.get(position).getPrice() + "/" + cate.get(position).getUnit());
                            adapter.notifyDataSetChanged();
                        }
                    });

                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            getDeta(getIntent().getStringExtra("pid"), cate.get(position).getFid());
                        }
                    });
                } catch (Exception r) {
                }
            }

        };
        recycler.setAdapter(adapter);
    }

    private void getDeta(String pid, String fip) {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("fid", fip);
        showDialog();

        XUtil.Post(Url.DECORATE_PACKAGEDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        SelectBean bean = gson.fromJson(result, SelectBean.class);
                        SelectBean.InfoEntity info1 = bean.getInfo();
                        startActivity(new Intent(SelectProjectActivity.this, LookDActivity.class).putExtra("info1", info1));
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }


    private BaseRVAdapter adapter;
    private List<SeelBean.InfoEntity.CateEntity> cate = new ArrayList<>();

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("套餐选择");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setVisibility(View.VISIBLE);
        submit.setText("保存");
        findViewById(R.id.submit).setOnClickListener(this);

    }

    private String selectId = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                if ("".equals(selectId)) {
                    MyToast("请选择套餐");
                    return;
                }
                Log.e("selectId", selectId + "");
                Intent intent = getIntent();
                intent.putExtra("selectId", selectId);
                intent.putExtra("selectName", taocan.getText().toString() + "\t" + jushi1.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
}
